package FWK.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.apache.log4j.Logger;

import FWK.util.FwkViewsUtils;

public class FwkProgressBar extends JInternalFrame {
	private Logger logger = Logger.getLogger(FwkProgressBar.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 6519752913281698986L;
	private JLabel label;
	private JProgressBar pb;

	public FwkProgressBar(int max, String title, String pathImage) {
		super(title);
		this.setSize(new Dimension(200, 150));
		
		try {
			this.setFrameIcon(FwkViewsUtils.getIcon(pathImage, 25, 25));
		} catch (IOException e) {
			logger.warn(e);
		}
		pb = new JProgressBar(0, max);
		pb.setValue(0);
		pb.setStringPainted(true);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		label = new JLabel("");

		JPanel panel = new JPanel();
		panel.add(pb);

		JPanel panel1 = new JPanel();
		panel1.setLayout(new BorderLayout());
		panel1.add(panel, BorderLayout.NORTH);
		panel1.add(label, BorderLayout.CENTER);
		panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
		this.add(panel1);
	}

	public void finish(String msj) {
		Toolkit.getDefaultToolkit().beep();
		pb.setValue(0);
		String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + msj + "</b>" + "</font>"
				+ "</html>";
		label.setText(str);
		this.dispose();
	}

	public void setValue(int value, String newMsj) {
		if(newMsj != null){
			String str = "<html>" + "<font color=\"#008000\">" + "<b>" + newMsj + "</b>"
					+ "</font>" + "</html>";
			label.setText(str);
		}
		pb.setValue(value);
	}

	public void start(String msj) {
		this.setVisible(true);
		String str = "<html>" + "<font color=\"#008000\">" + "<b>" + msj + "</b>"
				+ "</font>" + "</html>";
		label.setText(str);
	}

	public void close() {
		logger.debug("se cierra la progress bar");
		this.setVisible(false);
		this.dispose();
	}
}
